<?php
namespace Home\Controller;

use Home\Util\lib\YZTokenClient;
use Util\AccessController;

class GoodsController extends AccessController {


    public function index() {
        $this->Tokey = $this->S('access_token');
        $this->display();
    }

    /*
     * 获取有赞所有商品数据
     * 更新商品库存价格
     */

    public function UpdateGoods_KC_JG() {
        $LS = Service('YZGoods', 'yz_goods');
        $where['goods_no'] = array('neq', '');
        $where['mallid'] = $this->S('mallid');
        $data = $LS->query($where, 'alias , goods_no');


        //少于100库存、
        $ksdata = array();
        $t = 0;
        $conn = mssql_conn();
        $sql = "select a.spbh,(b.kcshl-isnull(c.shl,0)) as kxkc,a.zdshj,a.ywjg,case  when isnull(a.zdshj,0)<>0 then a.zdshj    when  isnull(a.zdshj,0)=0 then a.ywjg   end as jg from  spkfk a(nolock) left join spkfjc b(nolock) on a.spid=b.spid left join (select spid,sum(shl) as shl from pf_djmx a,jzorder_hz b  where a.djbh=b.djbh and b.is_zx='否' AND b.djbh like 'xsg%'  group by spid) c  on a.spid=c.spid where  b.kcshl>0 and (b.kcshl-isnull(c.shl,0))<100";
        $sql = iconv('utf-8', 'GB2312//IGNORE', $sql);
        $res = sqlsrv_query($conn, $sql);
        $LS1 = Service('YZGoods', 'yz_goods');
        while ($row = sqlsrv_fetch_array($res)) {
            $yzdata = $LS1->find(array('goods_no' => trim($row['spbh']),'mallid'=>$this->S('mallid')));
            if ($yzdata['alias']) {
                $ksdata[$t]['spbh'] = trim($row['spbh']);
                $ksdata[$t]['kc'] = $row['kxkc'];
                if($this->S('mallid') == '19010781'){
                    $ksdata[$t]['jg'] = $row['ywjg'];
                }else{
                    $ksdata[$t]['jg'] = $row['zdshj'] > 0 ? $row['zdshj'] : $row['ywjg'];
                }
                $ksdata[$t]['alias'] = $yzdata['alias'];
                $t++;
            }
        }
        $LS = Service('YZGoods', 'erp_update_yz_goods');
        $LS->remove(1);
        $this->S('data',$data);
        $this->S('ksdata',$ksdata);
        $this->datacount = count($data);
        $this->ksdatacount = count($ksdata);
        $this->display();
    }

    /*
     * ajax 更新
     */
    public function AjaxUpdateGoods() {
        $this->S('access_token') ? 1 : $this->error('access_token失效，请重新点击跳转本地');
        $conn = mssql_conn();
        $datas = $this->S('data');
        $data = $datas[$_POST['id']];
        if ($data['goods_no']) {
            $sql = "select a.spbh,a.spmch,a.shpgg,a.shpchd,b.kcshl as kc,(b.kcshl-isnull(c.shl,0)) as kxkc,a.zdshj,a.ywjg,case when isnull(a.zdshj,0)<>0 then a.zdshj  when  isnull(a.zdshj,0)=0 then a.ywjg   end as jg from  spkfk a(nolock) left join spkfjc b(nolock) on a.spid=b.spid left join (select spid,sum(shl) as shl from pf_djmx a,jzorder_hz b where a.djbh=b.djbh and b.is_zx='否' AND b.djbh like 'xsg%'  group by spid) c  on a.spid=c.spid" .
                "  where a.spbh='" . $data['goods_no'] . "'";
            $sql = iconv('utf-8', 'GB2312//IGNORE', $sql);
            $res = sqlsrv_query($conn, $sql);
            while ($row = sqlsrv_fetch_array($res)) {
                $kc = $row['kxkc'];
                $xj = $row['kxkc'] < 100 ? 1 : 0;
                //商业获取业务价格
                if($this->S('mallid') == '19010781'){
                    $jg = $row['ywjg'];
                }else{
                    $jg = $row['zdshj'] >0 ?$row['zdshj'] :$row['ywjg'];
                }
                $alias = $data['alias'];
                $goods_no = $data['goods_no'];
                $data1['token'] = $this->S('access_token');
                $data1['kc'] = $kc;
                $data1['jg'] = $jg;
                $data1['goods_no'] = $goods_no;
                $data1['alias'] = $alias;
                $data1['xj'] = $xj;
                $url = 'http://www.yp511.com:8085/index.php/ERPAPI/UpdataYZGoods_API';
                $content = curl_file_get_contents($url, $data1);
                echo $content;
            }
        } else {
            //无内码跳过
            echo 1;
        }
    }

    /*
    * 快速ajax 更新
    */
    public function AjaxUpdateGoodsks() {
        $this->S('access_token') ? 1 : $this->error('access_token失效，请重新点击跳转本地');
        $datas = $this->S('ksdata');
        $data = $datas[$_POST['id']];
        if ($data['spbh']) {
            $alias = $data['alias'];
            $goods_no = $data['spbh'];
            $data1['token'] = $this->S('access_token');
            $data1['kc'] = $data['kc'];
            $data1['jg'] = $data['jg'];
            $data1['goods_no'] = $goods_no;
            $data1['alias'] = $alias;
            $data1['xj'] = 1;
            $url = 'http://www.yp511.com:8085/index.php/ERPAPI/UpdataYZGoods_API';
            $content = curl_file_get_contents($url, $data1);
            echo $content;
        } else {
            //无有赞内码跳过
            echo 1;
        }

    }

    /*
     *本次更新结果
     */
    public function UpdateRes() {
        $LS = Service('YZGoods', 'erp_update_yz_goods');
        $allnum = $LS->query(null,' COUNT(*) as num',null);
        $successnum = $LS->query(array('state'=>1),' COUNT(*) as num',null);
        $errornum = $LS->query(array('state'=>0),' COUNT(*) as num',null);
        $errordata= $LS->query(array('state'=>0),null,null);
        $this->allnum = $allnum;
        $this->successnum = $successnum;
        $this->errornum = $errornum;
        $this->errordata = $errordata;
        $this->display();
    }

}